import moment from 'moment';

export function getQuery(name) {
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}

export function getQueryString(name) {
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}

export function diffTime(clickTime, tkPaidTime) {
	let date1 = clickTime;
	let date2 = tkPaidTime;

	if (!date1 || !date2) {
		return '无';
	}

	let h = moment(date2).diff(date1, 'hour');
	let m = moment(date2).diff(date1, 'minute');
	let s = moment(date2).diff(date1, 'second');

	let label = '';

	if (h) {
		label += `${h}小时`;
	}

	if (m) {
		label += `${m % 60}分`;
	}

	if (s) {
		label += `${s % 60}秒`;
	}

	return label;
}

export function isWechat() {
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		return true;
	} else {
		return false;
	}
}

export function isArray(value) {
	if (typeof Array.isArray === 'function') {
		return Array.isArray(value);
	} else {
		return Object.prototype.toString.call(value) === '[object Array]';
	}
}

export function deepMerge(a, b) {
	let k;
	for (k in b) {
		a[k] = a[k] && a[k].toString() === '[object Object]' ? deepMerge(a[k], b[k]) : (a[k] = b[k]);
	}
	return a;
}
